﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace Ciripescu_WebRole.Entities
{
    /// <summary>
    /// Un mesaj direct intre 2 useri sau un reply la un cirip
    /// Orice reply la un cirip exista si in tabela asta o copie (care are separat cu destinatarul ca PartitionKey sa se poata accesa repede),
    /// dar si in tabela Cirip ca sa putem extrage reply-urile impreuna cu Ciripurile normale
    /// fara inca o interogare in plus
    /// </summary>
    public class CiripMessage :TableStorageEntity
    {
        // partition key = Destinatarul mesajului

        public string From { get; set; }
        public string Content { get; set; }   
        /// <summary>
        /// Doar pt reply-uri! Pentru mesaje direct va fi null
        /// </summary>
        public string InReplyToId { get; set; }
        // don't use TimeStamp because we may not be able to backup it
        public DateTime TimePosted { get; set; }
        /// <summary>
        /// 0 for Direct Message (Inbox), 1 for Reply, 2 for Sent Message (Outbox - Sent)
        /// </summary>
        public int MessageType { get; set; }
        public string Version { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="from">The user that sent the cirip/reply  - Warning: for Outbox messages to and from are reversed!</param>
        /// <param name="to">The user who will read it (ParitionKey) - Warning: for Outbox messages to and from are reversed!</param>
        /// <param name="content"></param>
        /// <param name="type">0 for Direct Message (Inbox), 1 for Reply, 2 for Sent Message (Outbox - Sent)</param>
        /// <param name="inReplyToId">The original RowKey of the Cirip object</param>
        public CiripMessage(string from, string to, string content, int type, string inReplyToId)
            : base(to, string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks))
        {            
            this.Content = content;
            this.MessageType = type;
            this.InReplyToId = inReplyToId;
            this.From = from;
            this.Version = Properties.Settings.Default.Version;
            this.TimePosted = DateTime.Now;
            
        }
        public CiripMessage() { }
        public string getTo()
        {
            return PartitionKey;
        }
    }
}
